<?php
namespace Admin\Controller;

class PayLogController extends Controller {

    public function index() {
        $m    = M('vPayLog');
        $page = $m->where($this->indexExpr)->order($this->sortExpr)->page2();
        $yfMoney = 0;   //应付金额
        $sfMoney = 0;   //实付金额

        if(!empty($page['record'])) {
            foreach($page['record'] as $v) {
                $yfMoney = bcadd($yfMoney, $v['p_l_real_money'], C('MONEY_SCALE'));
                $sfMoney = bcadd($sfMoney, $v['p_l_pay_money'],  2);
            }
        }

        $this->assign('page', $page);
        $this->assign('yfMoney', $yfMoney)->assign('sfMoney', $sfMoney);
        $this->display();
    }

    /**
     * index0
     * 待支付概览
     *
     * @return void
     */
    public function index0() {
        $m     = M('v_pay_log_0_index');
        $page  = $m->where($this->indexExpr)->page2();
        $total = M('v_pay_log_0_total')->find();

        $this->assign(['page' => $page, 'total' => $total])
             ->display();
    }

    public function modify() {
        $id = empty($_GET['id']) ? 0 : (int)$_GET['id'];

        if(empty($id)) {
            throw new ThinkException('参数不完整，无法获取数据');
        }

        $m = D2();

        if(!$record = $m->getByZoneId($id)) {
            throw new ThinkException('没有找到广告位信息');
        }

        $mAd = D2('Ad');
        $rAd = $mAd->getAdsByTypeAndSize($record['zone_type'], $record['zone_width'], $record['zone_height']);

        $this->assign('ad', $rAd);
        $this->assign('record', $record);
        $this->display();
    }

    public function doSave() {
        $d   = $_POST;
        $dao = array();
        $m   = D2();

        $dao['p_l_id'] = I('post.id', 0, 'intval');

        if(isset($d['scale'])) {
            $dao['p_l_scale'] = I('post.scale', 0, 'intval');
        }

        if(isset($d['pay_money'])) {
            $dao['p_l_pay_money'] = I('post.pay_money', 0, 'floatval');
        }

        if(isset($d['comment'])) {
            $dao['p_l_comment'] = I('post.comment', '');
        }

        if(isset($d['status'])) {
            $dao['p_l_status'] = I('post.status', 0, 'intval');
        }

        try {
            $r = $m->save2($dao) ? array('s' => true, 'm' => '保存成功') : array('s' => false, 'm' => '保存出错');
        } catch (Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($r);
    }

    public function del() {
        $id = empty($_GET['id']) ? 0 : (int)$_GET['id'];

        if(empty($id)) {
            showJson(array('s' => false, 'm' => '参数不完整，无法删除数据'));
        }

        $m = D2();
        $r = $m->del($id) ? array('s' => true, 'm' => '删除成功') : array('s' => false, 'm' => '删除出错');

        showJson($r);
    }

    /**
     * 导出以下数据
     */
    public function export() {
        parent::_before_index();

        $m      = M('vPayLog');
        $record = array();

        $field = array(
                't0'            => '批次号',
                'user_name'     => '订单号',
                'accountname'   => '账号名称',
                'bankacc'       => '银行账号',
                'bank'          => '开户银行',
                'bankname'      => '地址',
                'p_l_pay_money' => '金额',
        );

        $exportHandler = new CExportToCsv;
        $exportHandler->setField($field);
        $exportHandler->setFile('结算支付数据');

        /**
         * 每次查询的数据量 
         */
        $limit  = 5000;
        $offset = 0;
        $id     = 0;

        while(1) {
            if(!$record = $m->where($this->indexExpr)->order($this->sortExpr)->limit("{$offset}, {$limit}")->select()) {
                break;
            }
        
            foreach($record as $k => $v) {
                $tmp = array();

                foreach($field as $k2 => $v2) {
                    $tmp[$k2] = isset($v[$k2])  ?   $v[$k2] :   '';
                }

                $tmp['t0']   = ++$id;
                $tmp['bank'] = getBankNameCn($v['bank']);

                $exportHandler->add($tmp);
            }

            $offset += $limit;
        }

        $exportHandler->export();
    }

    /**
     * doCleanPayLog
     * 清理上月之前所有支付数据
     *
     * @return void
     */
    public function doCleanPayLog() {
        $day  = date('Y-m-01', strtotime('-1 months'));
        $m    = D2();
        $expr = "p_l_add_date<'{$day}' AND p_l_status=1"; //清除上月之前已经支付的数据 

        $m->where($expr)->delete();

        showJson(array('s' => true, 'm' => '清理成功'));
    }

}
